package com.ruoyi.wmsorder.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.wmsorder.domain.WmsPurchaseorder;
import com.ruoyi.wmsorder.domain.WmsPurchaseorderDetail;
import com.ruoyi.wmsorder.domain.WmsSaleOrderDetail;
import com.ruoyi.wmsorder.domain.WmsSaleorder;
import com.ruoyi.wmsorder.domain.vo.*;
import com.ruoyi.wmsorder.mapper.WmsSaleOrderMapper;
import com.ruoyi.wmsorder.quartz.config.OderNos;
import com.ruoyi.wmsorder.quartz.config.SerialNumber;
import com.ruoyi.wmsorder.service.IWmsSaleorderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 销售订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-12
 */
@Service
@Slf4j
public class WmsSaleorderServiceImpl implements IWmsSaleorderService
{
    @Autowired
    private WmsSaleOrderMapper  wmsSaleOrderMapper;
    /**
     * 查询销售订单明细
     * 
     * @param id 销售订单主键
     * @return 销售订单
     */
    @Override
    public List<WmsSaleOrderDetailVO> selectWmsSaleorderById(Long id)
    {


        System.out.println("子表内容："+wmsSaleOrderMapper.selectWmsSaleOrderById(id));
        return wmsSaleOrderMapper.selectWmsSaleOrderById(id);
    }

    /**
     * 查询销售订单列表
     * 
     * @param wmsSaleorder 销售订单
     * @return 销售订单
     */
    @Override
    public List<WmsSaleorderVO>selectWmsSaleorderList(WmsSaleorder wmsSaleorder)
    {

        return wmsSaleOrderMapper.selectWmsSaleOrderList(wmsSaleorder);
    }

    /**
     * 查询销售订单弹窗列表
     * @param wmsSaleorder
     * @return
     */

    @Override
    public List<WmsSaleorderPoPVO> selectWmsSaleorderPopList(WmsSaleorder wmsSaleorder) {

        log.error("查询销售订单弹窗列表"+wmsSaleOrderMapper.selectWmsSaleorderPoPList(wmsSaleorder));
        return wmsSaleOrderMapper.selectWmsSaleorderPoPList(wmsSaleorder);
    }

    /**
     * 新增销售订单
     * 
     * @param wmsSaleorder 销售订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsSaleorder(WmsSaleorder wmsSaleorder)
    {


        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        Date date = new Date();
        String orderNo = OderNos.generateOrderNo(SerialNumber.PURCHASE_PREFIX, "yyyyMMdd");
        wmsSaleorder.setOrderNo(orderNo);
        wmsSaleorder.setCreateTime(DateUtils.getNowDate());
        wmsSaleorder.setUpdateTime(DateUtils.getNowDate());
        int rows = wmsSaleOrderMapper.insertWmsSaleOrder(wmsSaleorder);
        insertWmsSaleorderDetail(wmsSaleorder);
        return rows;
    }

    /**
     * 修改销售订单
     * 
     * @param wmsSaleorder 销售订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsSaleorder(WmsSaleorder wmsSaleorder)
    {
        wmsSaleorder.setUpdateTime(DateUtils.getNowDate());
        wmsSaleOrderMapper.deleteWmsSaleOrderDetailByOrderId(wmsSaleorder.getId());
        insertWmsSaleorderDetail(wmsSaleorder);
        return wmsSaleOrderMapper.updateWmsSaleOrder(wmsSaleorder);
    }

    /**
     * 批量删除销售订单
     * 
     * @param ids 需要删除的销售订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsSaleorderByIds(Long[] ids)
    {
        wmsSaleOrderMapper.deleteWmsSaleOrderDetailByOrderIds(ids);
        return wmsSaleOrderMapper.deleteWmsSaleOrderByIds(ids);
    }

    /**
     * 删除销售订单信息
     * 
     * @param id 销售订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsSaleorderById(Long id)
    {
        wmsSaleOrderMapper.deleteWmsSaleOrderDetailByOrderId(id);
        return wmsSaleOrderMapper.deleteWmsSaleOrderById(id);
    }

    /**
     * 新增销售订单详情信息
     * 
     * @param wmsSaleorder 销售订单对象
     */
    public void insertWmsSaleorderDetail(WmsSaleorder wmsSaleorder)
    {
        List<WmsSaleOrderDetail> wmsSaleOrderDetails = wmsSaleorder.getWmsSaleOrderDetails();
        //主表id
        Long id = wmsSaleorder.getId();


        if (StringUtils.isNotNull(wmsSaleOrderDetails))
        {
            ArrayList<WmsSaleOrderDetail> list = new ArrayList<>();
            for (WmsSaleOrderDetail wmsSaleOrderDetail : wmsSaleOrderDetails)
            {
                wmsSaleOrderDetail.setOrderId(id);
                wmsSaleOrderDetail.setCreateTime(DateUtils.getNowDate());
                wmsSaleOrderDetail.setUpdateTime(DateUtils.getNowDate());
                wmsSaleOrderDetail.setCreateById(getUserId());
                wmsSaleOrderDetail.setUpdateById(getUserId());
                list.add(wmsSaleOrderDetail);
            }
            if (list.size() > 0)
            {
                wmsSaleOrderMapper.batchWmsSaleOrderDetail(list);
            }
        }
    }


}
